package com.central.user.handler

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.date.DateUnit
import cn.hutool.core.date.DateUtil
import com.central.common.enum.AttentionTypeEnum
import com.central.common.feign.AttentionService
import com.central.common.feign.PostService
import com.central.common.feign.VisitService
import com.central.common.model.CommonResult
import com.central.common.model.SysUser
import com.central.common.model.success
import com.central.user.service.ISysUserService
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

@ApiModel("用户信息")
data class UserDetailVo(

    @ApiModelProperty("用户id") var id: Long? = null,
    @ApiModelProperty("用户背景图片") var backgroundPic: String? = "",
    @ApiModelProperty("真实姓名") var username: String? = "",
    @ApiModelProperty("用户头像") var headImgUrl: String? = "",
    @ApiModelProperty("性别") var sex: Int? = 0,
    @ApiModelProperty("加入的天数") var joinDay: Long? = null,
    @ApiModelProperty("个人简介") var description: String? = null,
    @ApiModelProperty("获赞") var likeCount: Long? = 0,
    @ApiModelProperty("关注数量") var attentionCount: Long? = 0,
    @ApiModelProperty("粉丝数量") var fansCount: Long? = 0,
    @ApiModelProperty("来访数量") var visitCount: Long? = 0,
)

@ApiModel("用户列表信息")
data class UserInfo(

    @ApiModelProperty("用户id") var id: Long? = null,
    @ApiModelProperty("真实姓名") var username: String? = "",
    @ApiModelProperty("用户头像") var headImgUrl: String? = "",
    @ApiModelProperty("性别") var sex: Int? = 0,
    @ApiModelProperty("加入的天数") var joinDay: Long? = null,
)


@ApiModel("用户信息修改参数")
data class UpdateUserInfoDto(

    @ApiModelProperty("真实姓名") var username: String? = "",
    @ApiModelProperty("用户头像") var headImgUrl: String? = "",
    @ApiModelProperty("用户背景图片") var backgroundPic: String? = "",
    @ApiModelProperty("性别") var sex: Int? = 0,
    @ApiModelProperty("手机号") var phone: String? = null,
    @ApiModelProperty("个人简介") var description: String? = null,
)
interface UserHandler{

    /**
     * 获取app用户信息
     */
    fun getAppUserInfo(sysUser: SysUser): CommonResult<UserDetailVo>

    /**
     * 修改用户背景图片
     */
    fun updateUserBackGroundPic(backPic: String, sysUser: SysUser): CommonResult<Boolean>

    /**
     * 修改用户信息
     */
    fun updateUserInfo(updateUserInfoDto: UpdateUserInfoDto, sysUser: SysUser): CommonResult<Boolean>

    /**
     * 获取用户信息
     */
    fun getUserInfo(sysUser: SysUser): CommonResult<UserInfo>
}

@Service
open class UserHandleImp : UserHandler {

    @Autowired
    private lateinit var attentionService: AttentionService

    @Autowired
    private lateinit var visitService: VisitService

    @Autowired
    private lateinit var postService: PostService

    @Autowired
    private lateinit var userService: ISysUserService

    override fun getAppUserInfo(sysUser: SysUser): CommonResult<UserDetailVo> = success(UserDetailVo().apply {
        BeanUtil.copyProperties(sysUser, this)
        joinDay = DateUtil.between(sysUser.createTime, Date(), DateUnit.DAY)
        likeCount = postService.getMyPostLikeNum(sysUser.id)
        attentionCount = attentionService.getMyAttentionIdList(sysUser.id, AttentionTypeEnum.USER)?.size?.toLong() ?: 0
        fansCount = attentionService.getMyFansUserId(sysUser.id)?.size?.toLong() ?: 0
        visitCount = visitService.getMyVisitUserId(sysUser.id)?.size?.toLong() ?: 0
    }, "获取我的个人信息成功")

    override fun updateUserBackGroundPic(backPic: String, sysUser: SysUser): CommonResult<Boolean> = success(

        userService.getById(sysUser.id)?.run {
            backgroundPic = backPic
            userService.updateById(this)
        } ?: false
    , "更新背景图片成功")

    override fun updateUserInfo(updateUserInfoDto: UpdateUserInfoDto, sysUser: SysUser): CommonResult<Boolean> = success(

        userService.getById(sysUser.id)?.run {
            BeanUtil.copyProperties(updateUserInfoDto, this)
            userService.updateById(this)
        } ?: false
        , "更新背景图片成功")

    override fun getUserInfo(sysUser: SysUser): CommonResult<UserInfo> = success(

        UserInfo().apply {
            BeanUtil.copyProperties(sysUser, this)
            joinDay = DateUtil.between(sysUser.createTime, Date(), DateUnit.DAY)
        }
    , "获取用户信息成功")

}

